package com.dmxh.service.impl;

import com.dmxh.mapper.TbContentCategoryMapper;
import com.dmxh.params.QueryParams;
import com.dmxh.pojo.TbContentCategory;
import com.dmxh.pojo.TbContentCategoryExample;
import com.dmxh.pojo.TbContentExample;
import com.dmxh.result.PageList;
import com.dmxh.service.ContentItemService;
import com.dmxh.vo.ContentCategoryVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ContentItemServiceImpl implements ContentItemService {

    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;

    @Override
    public PageList<ContentCategoryVo> queryPage(QueryParams queryParams) {
        Page<TbContentCategory> page = PageHelper.startPage(queryParams.getPageNum(), queryParams.getPageSize());
        List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(null);
        PageInfo<TbContentCategory> pageInfo = page.toPageInfo();
        List<ContentCategoryVo> collect = list.stream().map(tbContentCategory -> {
            ContentCategoryVo contentCategoryVo = new ContentCategoryVo();
            BeanUtils.copyProperties(tbContentCategory, contentCategoryVo);
            return contentCategoryVo;
        }).collect(Collectors.toList());
        return new PageList<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), collect);
    }

    @Override
    public List<ContentCategoryVo> getAllContentItem() {
        List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(null);
        List<ContentCategoryVo> collect = list.stream().map(tbContentCategory -> {
            ContentCategoryVo contentCategoryVo = new ContentCategoryVo();
            BeanUtils.copyProperties(tbContentCategory, contentCategoryVo);
            return contentCategoryVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void addInfo(ContentCategoryVo contentCategoryVo) {
        TbContentCategory tbContentCategory = new TbContentCategory();
        BeanUtils.copyProperties(contentCategoryVo, tbContentCategory);
        tbContentCategoryMapper.insertSelective(tbContentCategory);
    }

    @Override
    public void updateInfo(ContentCategoryVo contentCategoryVo) {
        TbContentCategory tbContentCategory = new TbContentCategory();
        BeanUtils.copyProperties(contentCategoryVo, tbContentCategory);
        tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);
    }

    @Override
    public void deleteById(long id) {
        tbContentCategoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteBatch(Integer[] ids) {
        List<Integer> list = Arrays.asList(ids);
        List<Long> collect = list.stream().map(Integer::longValue).collect(Collectors.toList());
        TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
        tbContentCategoryExample.createCriteria().andIdIn(collect);
        tbContentCategoryMapper.deleteByExample(tbContentCategoryExample);
    }
}
